package com.yanq.liancar.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.vondear.rxtool.RxActivityTool;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.util.GlideImageLoader;
import com.yanq.liancar.util.Utils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallCRescueActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_rescue)
    TextView tvRescue;
    @BindView(R.id.tv_trailer)
    TextView tvTrailer;
    @BindView(R.id.tv_electric)
    TextView tvElectric;
    @BindView(R.id.tv_unlock)
    TextView tvUnlock;
    @BindView(R.id.tv_battery)
    TextView tvBattery;
    @BindView(R.id.call_rescue)
    TextView callRescue;
    @BindView(R.id.img_gif)
    ImageView mImageView;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_see)
    TextView tvSee;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.tv_peoNum)
    TextView tv_peoNum;
    private List<Integer> imageUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_samllc_rescue);
        Utils.setStatusBar(this, true, false);
        ButterKnife.bind(this);
        mTvTitle.setText("急速救援");
        initBannerData();
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("记录");
        mTvRight.setTextColor(Color.parseColor("#2987e9"));

        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxActivityTool.skipActivity(mContext, SmallCRescueRecordActivity.class);     //救援记录
            }
        });

        SpannableString spannableString = new SpannableString("已成功为165820位用户保驾护航");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.background_content));
        spannableString.setSpan(colorSpan, 10, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan colorSpan3 = new ForegroundColorSpan(getResources().getColor(R.color.background_content));
        spannableString.setSpan(colorSpan3, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        RelativeSizeSpan sizeSpan03 = new RelativeSizeSpan(1.6f);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(getResources().getColor(R.color.red));
        spannableString.setSpan(sizeSpan03, 4, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_peoNum.setText(spannableString);

        RequestOptions options = new RequestOptions();
        options.centerCrop();
        options.priority(Priority.HIGH);
        options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        Glide.with(mContext).load(R.drawable.gif).listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                return false;
            }
        }).apply(options).into(mImageView);


    }

    private void initBannerData() {
        imageUrl = new ArrayList<>();
        imageUrl.add(R.mipmap.banner1);
        imageUrl.add(R.mipmap.banner2);
        imageUrl.add(R.mipmap.banner3);
        imageUrl.add(R.mipmap.banner4);
        imageUrl.add(R.mipmap.banner5);
        imageUrl.add(R.mipmap.banner6);
        banner.setImageLoader(new GlideImageLoader())
                .setImages(imageUrl)//设置图片集合
                .setDelayTime(3000)//设置轮播时间
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//设置指示器位置（当banner模式中有指示器时）
                .setIndicatorGravity(BannerConfig.CENTER)
                .start();//banner设置方法全部调用完毕时最后调用
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //ToastUtils.showToast(SmallBGoodsDetailsActivity.this, "您点击了第" + (mBanners.get(position).type + 1) + "张照片");
            }
        });
    }

    @OnClick({R.id.tv_rescue, R.id.tv_trailer, R.id.tv_electric, R.id.tv_unlock, R.id.tv_battery, R.id.call_rescue, R.id.tv_see})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_rescue:
                break;
            case R.id.tv_trailer:
                break;
            case R.id.tv_electric:
                break;
            case R.id.tv_unlock:
                break;
            case R.id.tv_battery:
                break;
            case R.id.call_rescue:
//                Intent intent = new Intent(Intent.ACTION_CALL);
//                Uri data = Uri.parse("tel:" + "17630721477");
//                intent.setData(data);
//                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//                    // TODO: Consider calling
//                    //    ActivityCompat#requestPermissions
//                    // here to request the missing permissions, and then overriding
//                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                    //                                          int[] grantResults)
//                    // to handle the case where the user grants the permission. See the documentation
//                    // for ActivityCompat#requestPermissions for more details.
//                    return;
//                }
//                startActivity(intent);
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4008055011"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.tv_see:
                RxActivityTool.skipActivity(this, RescueDetailsActivity.class);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        banner.stopAutoPlay();
    }

}
